package com.example.gaojt.androiddemo.boostDemo.aa;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;

import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class MemoryInfoHelper {

    private static long sTotalMemByte = -1;
    private static Method sMethodGetTotalPss = null;
    private static Method sMethodGetProcessMemoryInfo = null;
    // 1G作为判断是否是低端机的标志
    // private static final long LOW_END_PHONE = 1 * 1024 * 1024 * 1024L;
    private static final long LOW_END_PHONE_512 = 512 * 1024 * 1024L;


    /** 获取当前内存剩余值， 单位byte */
    public static long getAvailableMemoryByte() {
        return 0;
    }

    /** 获取当前总内存值，单位byte */
    public static long getTotalMemoryByte() {
        return 0;
    }

    /**
     * 把 所有 关联的PID 的内存值加起来，单位是byte
     *
     */
    public static long getProcessMemory(ActivityManager am, ArrayList<Integer> pids) {

        if (pids == null || pids.size() == 0) {
            return 0;
        }

        int pidCount = pids.size();
        int[] ipids = new int[pidCount];
        long memory = 0;

        for (int i = 0; i < pidCount; i++) {
            ipids[i] = pids.get(i);
        }

        try {
            Debug.MemoryInfo[] memoryInfoArray = getMemoryInfosByPids(am, ipids);
            if (memoryInfoArray != null && memoryInfoArray.length > 0) {
                for (Debug.MemoryInfo mInfo : memoryInfoArray) {
                    memory += getTotalPssMemory(mInfo);
                }
            }
        } catch (Exception e) {}
        return memory * 1024;
    }


    /**
     * 反射获得占用内存大小
     */
    private static int getTotalPssMemory(Debug.MemoryInfo mi) {
        try {
            if (sMethodGetTotalPss == null) {
                sMethodGetTotalPss = mi.getClass().getMethod("getTotalPss");
            }
            return (Integer) sMethodGetTotalPss.invoke(mi);
        } catch (Exception e) {}
        return 0;
    }

    /**
     * 反射获得MemoryInfo
     */
    private static Debug.MemoryInfo[] getMemoryInfosByPids(ActivityManager am, int[] pids) {

        try {
            if (sMethodGetProcessMemoryInfo == null) {
                sMethodGetProcessMemoryInfo = ActivityManager.class.getMethod("getProcessMemoryInfo", int[].class);
            }
            return (Debug.MemoryInfo[]) sMethodGetProcessMemoryInfo.invoke(am, pids);
        } catch (Exception e) {}

        return null;
    }


    public long getAvailMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem;
    }

    public static long getRamSize() {
        // GB
        String path = "/proc/meminfo";
        String firstLine = null;
        long totalRam = 0;
        try {
            FileReader fileReader = new FileReader(path);
            BufferedReader br = new BufferedReader(fileReader, 8192);
            firstLine = br.readLine().split("\\s+")[1];
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (firstLine != null) {
            totalRam = Long.valueOf(firstLine) * 1024;
        }
        return totalRam;
    }

}
